package per.xue.carid_booking.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import per.xue.carid_booking.base.utils.ResultUtil;
import per.xue.carid_booking.base.dto.ResultDTO;
import per.xue.carid_booking.base.page.PageList;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import per.xue.carid_booking.utils.TicketStateCode;
import per.xue.carid_booking.dto.respondDTO.CarRespondDTO;
import per.xue.carid_booking.dto.respondDTO.TicketRespondDTO;
import per.xue.carid_booking.entity.Car;
import per.xue.carid_booking.entity.Station;
import per.xue.carid_booking.service.CarService;
import per.xue.carid_booking.service.StationService;
import per.xue.carid_booking.service.TicketService;
import per.xue.carid_booking.entity.Ticket;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 *
 * @author ${author}
 * @since 2021-05-22
 */
@RestController
@RequestMapping("/ticket")
@CrossOrigin
@Slf4j
public class TicketController {

        @Autowired
        private TicketService ticketService;

        @Autowired
        private CarService carService;

        @Autowired
        private StationService stationService;

        @PostMapping("/UserBuyTicket")
        @ApiOperation(value = "用户购买车票")
/*        @ApiImplicitParams({
                @ApiImplicitParam(name = "userId", dataType = "string",  required = true, value = "用户id"),
                @ApiImplicitParam(name = "name", dataType = "string",  required = true, value = "乘客名"),
                @ApiImplicitParam(name = "idcard", dataType = "string",  required = true, value = "身份证号"),
                @ApiImplicitParam(name = "gender", dataType = "string",  required = true, value = "性别，0是女，1是男"),
                @ApiImplicitParam(name = "callphone", dataType = "string",  required = true, value = "联系方式"),
                @ApiImplicitParam(name = "carId", dataType = "string",  required = true, value = "车次id"),
        })*/
        public ResultDTO insertOne( @RequestBody Ticket ticket)throws Exception{
            ticket.setStats(TicketStateCode.RELEASE);
            try {
                ticketService.save(ticket);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(ticket);
        }


        @PostMapping("/UserPayTicket")
        @ApiOperation(value = "用户支付车票")
/*        @ApiImplicitParams({
                @ApiImplicitParam(name = "ticketId", dataType = "string",  required = true, value = "车票id"),
        })*/
        public ResultDTO UserPayTicket( @RequestBody int ticketId)throws Exception{

            try {
                Ticket ticket = ticketService.get(ticketId);
                if (!ticket.getStats().equals( TicketStateCode.RELEASE)) {
                    return ResultUtil.Error("500","该车票不是未支付状态");
                }
                ticket.setStats(TicketStateCode.PAYED);
                ticketService.update(ticket);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(ticketService.get(ticketId));
        }

        @PostMapping("/AdminCancelTicket")
        @ApiOperation(value = "管理员退票票")
/*        @ApiImplicitParams({
                @ApiImplicitParam(name = "ticketId", dataType = "string",  required = true, value = "车票id"),
        })*/
        public ResultDTO AdminCancelTicket( @RequestBody int ticketId)throws Exception{
            try {
                Ticket ticket = ticketService.get(ticketId);
                if (!ticket.getStats().equals(TicketStateCode.PAYED)) {
                    return ResultUtil.Error("500","该车票未支付");
                }
                Car car = carService.get(ticket.getCarId());
                if (car.getTime().compareTo(new Date())==-1) {
                    return ResultUtil.Error("500","该车次已发车");
                }
                ticket.setStats(TicketStateCode.CHANCEL);
                ticketService.update(ticket);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(ticketService.get(ticketId));
        }


        @PostMapping("/updateById")
        @ApiOperation(value = "按id修改数据")
        //@ApiImplicitParams()
        public ResultDTO updateById( @RequestBody Ticket ticket)throws Exception{
            try {
                ticketService.update(ticket);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(ticketService.get(ticket.getTicketId()));

        }

        @GetMapping("/deleteById/{id}")
        @ApiOperation("按id删除数据")
/*        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "实体类id"),

        })*/
        public ResultDTO deleteById( @PathVariable("id") String id)throws Exception{
            Ticket oldTicket = null;
            try {
                oldTicket = ticketService.get(id);
                ticketService.deleteById(id);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(oldTicket);
        }

        @PostMapping("/getTicketDTOPage")
        @ApiOperation("按条件查询，分页")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO getTicketDTOPage(  @RequestBody Ticket ticket,
                                              int pageNum,
                                              int pageSize)throws Exception{
            PageList<TicketRespondDTO> resultPageList = null;
            try {
                /*System.out.println(ticket);*/
                PageList<Ticket> pageData = ticketService.findPageData(ticket, pageNum, pageSize);
                List<Ticket> data = pageData.getData();
                List<TicketRespondDTO> result = new ArrayList<>();
                for (Ticket item : data) {
                    Car car = carService.get(item.getCarId());
                    Station startStation = stationService.get(car.getStartStationId());
                    Station endStation = stationService.get(car.getStartStationId());
                    CarRespondDTO carRespondDTO = new CarRespondDTO(car, startStation.getStationName(), endStation.getStationName());
                    TicketRespondDTO ticketRespondDTO = new TicketRespondDTO(item, carRespondDTO);
                    result.add(ticketRespondDTO);
                }
                resultPageList = new PageList<>(result, pageData.getPageNum(), pageData.getPageSize(), pageData.getTotalItems());
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(resultPageList);

        }

        @PostMapping("/getTicketDTO")
        @ApiOperation("按条件查询，不分页")
        //@ApiImplicitParams()
        public ResultDTO getTicketDTO( @RequestBody Ticket ticket)throws Exception{
            List<TicketRespondDTO> result = new ArrayList<>();
            try {
                List<Ticket> ticketList = ticketService.findByParams(ticket);
                for (Ticket item : ticketList) {
                    Car car = carService.get(item.getCarId());
                    Station startStation = stationService.get(car.getStartStationId());
                    Station endStation = stationService.get(car.getStartStationId());
                    CarRespondDTO carRespondDTO = new CarRespondDTO(car, startStation.getStationName(), endStation.getStationName());
                    /*System.out.println(item);
                    System.out.println(carRespondDTO);*/
                    TicketRespondDTO ticketRespondDTO = new TicketRespondDTO(item, carRespondDTO);
                    result.add(ticketRespondDTO);
                }
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(result);
        }


        @PostMapping("/getTicketPage")
        @ApiOperation("按条件查询，分页")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO selectEntityPage(  @RequestBody  Ticket ticket,
                                            int pageNum,
                                            int pageSize)throws Exception{
            PageList<Ticket> pageData = null;
            try {
                pageData = ticketService.findPageData(ticket,pageNum,pageSize);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);

        }

        @PostMapping("/getTicket")
        @ApiOperation("按条件查询，不分页")
        //@ApiImplicitParams()
        public ResultDTO selectEntity( @RequestBody Ticket ticket)throws Exception{
            List<Ticket> ticketList = null;
            List<TicketRespondDTO> result = new ArrayList<>();
            try {
                ticketList = ticketService.findByParams(ticket);
                for (Ticket item : ticketList) {
                    Car car = carService.get(item.getCarId());
                    Station startStation = stationService.get(car.getStartStationId());
                    Station endStation = stationService.get(car.getStartStationId());
                    CarRespondDTO carRespondDTO = new CarRespondDTO(car, startStation.getStationName(), endStation.getStationName());
                    /*System.out.println(item);
                    System.out.println(carRespondDTO);*/
                    TicketRespondDTO ticketRespondDTO = new TicketRespondDTO(item, carRespondDTO);
                    result.add(ticketRespondDTO);
                }
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(result);
        }

}
